package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiNews;
import cn.com.api.service.ApiNewsService;
import cn.com.api.service.ApiT_log_userService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "news/")
public class ApiNewsController {
	@Autowired
	ApiNewsService apiNewsService;
	@Autowired
	ApiT_log_userService logUserService;

	// 广告位
	@RequestMapping("/findNewsAd")
	public void findNewsAd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String ctype = request.getParameter("ctype");
		String city_id = request.getParameter("city_id");
		Map<String, Object> map = new HashMap<String, Object>();

		List<ApiNews> list = apiNewsService.findNewsAd(ctype,
				Integer.valueOf(city_id));

		response.setCharacterEncoding("UTF-8");
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "成功！");
		map.put("datas", list);
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 关于我们
	@Transactional
	@RequestMapping("/ApiAboutus")
	public void ApiAboutus(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String ctype = request.getParameter("ctype");
		List<ApiNews> datas = apiNewsService.ApiAboutus(ctype);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 帮助中心
	@RequestMapping("/help")
	public void help(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String ctype = request.getParameter("ctype");

		List<ApiNews> datas = apiNewsService.help(ctype);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 查看帮助内容
	@RequestMapping("/helpContent")
	public void helpContent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("rid");
		Integer rid = Integer.parseInt(id);
		List<ApiNews> datas = apiNewsService.helpContent(rid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	// 查看帮助内容
	@RequestMapping("/bannerClickStat")
	public void bannerClickStat(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
		String bid=request.getParameter("bannerid");
		Map<String,Object> map=new HashMap<String,Object>();
		String cityid=request.getParameter("cityid");
		logUserService.bannerClickStat(bid, cityid);
		PrintWriter out = response.getWriter();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}
